﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Zero.UControl.Component.Command;

namespace Zero.UControl.Demo.ViewModels
{
    public class FormViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        public event PropertyChangedEventHandler? PropertyChanged;

        #region 构造函数
        public FormViewModel() 
        {
            DoLoginCommand = new BaseCommand<object>(DoLogin);
        }

        #endregion

        #region 成员属性
        private string username;

        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Username"));
            }
        }

        private string password;

        public string Password
        {
            get { return password; }
            set
            {
                password = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Password"));
            }
        }
        #endregion

        #region 命令
        public ICommand DoLoginCommand { get; set; }

        #endregion

        #region 本地方法
        public string Error => "";

        public string this[string columnName]
        {
            get
            {
                if (columnName == nameof(Username))
                {
                    if (string.IsNullOrEmpty(Username))
                        return "用户名不能为空";
                    if (Username.Length < 6)
                        return "用户名至少6个字符";
                }
                else if (columnName == nameof(Password))
                {
                    if (string.IsNullOrEmpty(Password))
                        return "密码不能为空";
                    if (Password.Length < 6)
                        return "密码至少6个字符";
                }
                return "";
            }
        }


        private void DoLogin(object obj)
        {
            MessageBox.Show($"Username: {Username}, Password: {Password}");
        }
        #endregion
    }
}
